package com.components.project.ui.fragment

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.blankj.utilcode.util.ActivityUtils
import com.components.base.BaseFragmentAdapter
import com.components.base.layout.XCollapsingToolbarLayout
import com.components.base.titlebar.TitleBar
import com.components.project.MainActivity
import com.components.project.R
import com.components.project.ui.base.ProjectBaseFragment
import com.google.android.material.tabs.TabLayout
import com.gyf.immersionbar.ImmersionBar

class HomeFragment : ProjectBaseFragment<MainActivity>(),
    XCollapsingToolbarLayout.OnScrimsListener {

    private var xCollapsingToolbarLayout: XCollapsingToolbarLayout? = null
    private var tabLayout: TabLayout? = null
    private var viewPager: ViewPager? = null
    private var tvAddress: TextView? = null
    private var searchEditText: EditText? = null
    private var ivSearch: AppCompatImageView? = null
    private var titleBar: TitleBar? = null
    private var mToolbar: Toolbar? = null

    private var pagerAdapter: BaseFragmentAdapter<Fragment>? = null

    override fun getLayoutId(): Int {
        return R.layout.fragment_home
    }

    override fun initView() {
        titleBar = findViewById(R.id.titleBar)
        xCollapsingToolbarLayout = findViewById(R.id.xCollapsingToolbarLayout)

        tabLayout = findViewById(R.id.tabLayout)
        viewPager = findViewById(R.id.viewPager)
        tvAddress = findViewById(R.id.tvAddress)
        searchEditText = findViewById(R.id.searchEditText)
        ivSearch = findViewById(R.id.ivSearch)
        mToolbar = findViewById(R.id.tb_home_title)

        // 给这个 ToolBar 设置顶部内边距，才能和 TitleBar 进行对齐
        //这句话的目的是让列表滑动上去后titleBar不会去占领状态栏的位置
        ImmersionBar.setTitleBar(getAttachActivity(), mToolbar)

        xCollapsingToolbarLayout?.setOnScrimsListener(this)
    }

    override fun initData() {
        /**只有文字的tab*/
        pagerAdapter = BaseFragmentAdapter(this.childFragmentManager)
        pagerAdapter?.addFragment(ListInfoFragment(), "列表A")
        pagerAdapter?.addFragment(ListInfoFragment(), "列表B")
        pagerAdapter?.addFragment(ListInfoFragment(), "列表C")
        pagerAdapter?.setLazyMode(true)
        viewPager?.adapter = pagerAdapter
        tabLayout?.setupWithViewPager(viewPager)
    }

    override fun onResume() {
        super.onResume()
        getStatusBarConfig()?.statusBarDarkFont(false)?.init()
    }

    override fun statusBarDarkFont(): Boolean {
        xCollapsingToolbarLayout?.let {
            return it.mIsScrimsShown
        }
        return false
    }

    @SuppressLint("RestrictedApi")
    override fun onScrimsStateChange(layout: XCollapsingToolbarLayout?, shown: Boolean) {
        if (shown) {
            tvAddress?.setTextColor(
                ContextCompat.getColor(
                    ActivityUtils.getTopActivity(),
                    R.color.black
                )
            )
            searchEditText?.setBackgroundResource(R.drawable.home_search_bar_gray_bg)
            searchEditText?.setTextColor(
                ContextCompat.getColor(
                    ActivityUtils.getTopActivity(),
                    R.color.black60
                )
            )
            ivSearch?.supportImageTintList =
                ColorStateList.valueOf(ActivityUtils.getTopActivity().getColor(R.color.colorIcon))
            getStatusBarConfig()?.statusBarDarkFont(true)?.init()
        } else {
            tvAddress?.setTextColor(
                ContextCompat.getColor(
                    ActivityUtils.getTopActivity(),
                    R.color.white
                )
            )
            searchEditText?.setBackgroundResource(R.drawable.home_search_bar_transparent_bg)
            searchEditText?.setTextColor(
                ContextCompat.getColor(
                    ActivityUtils.getTopActivity(),
                    R.color.white60
                )
            )
            ivSearch?.supportImageTintList =
                ColorStateList.valueOf(ActivityUtils.getTopActivity().getColor(R.color.white))
            getStatusBarConfig()?.statusBarDarkFont(false)?.init()
        }
    }

}